package com.backend_pay.controller.income;

import com.backend_pay.entity.IncomeDetail;
import com.backend_pay.entity.User;
import com.backend_pay.service.IncomeDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

@Controller //展示层bean
public class incomeController {
    //自动注入incomeService
    @Autowired
    IncomeDetailService incomeDetailService;

    //增，增加一条收入
    @PutMapping(value="/addIncome")
    @ResponseBody
    public String addIncome(@RequestBody IncomeDetail data){
        try{
            incomeDetailService.addIncomeDetail(data.getUserid(),data.getTypeIncome(),data.getMoney(),data.getComment(),data.getCreatetime());
            return "true";
        }catch(Exception e){
            return e.getMessage();
        }
    }
    //删，删除一条收入记录By id
    @DeleteMapping(value="/deleteIncome/{id}")
    @ResponseBody
    public String deleteIncomeById(@PathVariable("id") Integer id){
        String result=incomeDetailService.deleteById(id);
        if(result.equals("done")){
            return "true";
        }else{
            return "false";
        }
    }
    //改，修改一条收入信息
    @PostMapping(value="/updateIncome")
    @ResponseBody
    public void updateIncome(@RequestBody IncomeDetail data){
        IncomeDetail incomeDetail;
        incomeDetail=data;
//        取消了修改收入信息时更新createTime
//        Timestamp createdTime=new Timestamp(System.currentTimeMillis());
//        incomeDetail.setCreatetime(createdTime);
        incomeDetailService.save(incomeDetail);
    }
    //查，查询所有收入记录By userid
    @GetMapping(value="/incomeFindAll/{userid}")
    @ResponseBody
    public List<IncomeDetail> findAllById(@PathVariable("userid") Integer userid){
        return incomeDetailService.findAllById(userid);
    }
    //查，按条件查询收入记录
    @PostMapping(value="/queryIncomeByCondition")
    @ResponseBody
    public List<IncomeDetail> queryIncomeByCondition(@RequestBody User user,
                                                     @RequestParam("typeIncome") String typeIncome,
                                                     @RequestParam("start") Timestamp start,
                                                     @RequestParam("end") Timestamp end){
        /*System.out.println("typeIncome:"+typeIncome);
        System.out.println("start:"+start);
        System.out.println("end:"+end);*/
        if(typeIncome.equals("empty")){
            return incomeDetailService.queryIncomeByConditionNoType(user.getId(),start,end);
        }
        return incomeDetailService.queryIncomeByCondition(user.getId(),typeIncome,start,end);
    }
    //查，按条件查询收入的金额、收入类型
    @GetMapping(value="/queryIncomeByDate/{id}")
    @ResponseBody
    public List<Object> queryIncomeByDate(@PathVariable("id") Integer userid,
                                              @RequestParam("start") Timestamp start,
                                              @RequestParam("end") Timestamp end){
        return incomeDetailService.queryIncomeByDate(userid,start,end);
    }
    //查，按条件查询收入的金额、时间
    @GetMapping(value="/queryIncomeGroupByDate/{id}")
    @ResponseBody
    public List<Object> queryIncomeGroupByDate(@PathVariable("id") Integer userid,
                                               @RequestParam("start") Timestamp start,
                                               @RequestParam("end") Timestamp end){
        return incomeDetailService.queryIncomeGroupByDate(userid,start,end);
    }
    //查所有收入类型
    @GetMapping(value="/allKindsOfIncome")
    @ResponseBody
    public String[] findAllKindOfIncome(){//ArrayList<String>
        return incomeDetailService.findAllKindOfIncome();
    }
}
